using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


namespace WinSAG
{
    public class WSF_Globals
    {
        private ProjectProperties projectProperties;
        private SAGPreferences sagPreferences;
        private Form mainForm;
        
        private WSF_ProgressForm progress = null;//new WSF_ProgressForm();
        private WSF_PropertiesDialog wsfPropertiesPage;        

        private bool projectDataDirty = false;
        private string currentTmpFile = null;
        private bool satGotKilled = false;

        public WSF_Globals()
        {
            //TODO: Make this a singleton
            projectProperties = null;
            wsfPropertiesPage = null; // new WSF_PropertiesDialog(projectProperties);
            sagPreferences = new SAGPreferences();            
        }


        /// <summary>
        /// Gets or sets a value indicating whether SAT got killed.
        /// </summary>
        /// <value><c>true</c> if SAT got killed forcefully; otherwise, <c>false</c>.</value>
        internal bool SATGotKilled
        {
            get { return satGotKilled; }
            set { satGotKilled = value; }
        }

        /// <summary>
        /// Gets or sets the temp file that is currently being used.
        /// </summary>
        /// <value>The TMP filename.</value>
        internal string TmpFile
        {
            get { return currentTmpFile; }
            set { currentTmpFile = value; }
        }

        /// <summary>
        /// Gets or sets the main WinSAG form.
        /// </summary>
        /// <value>The main form.</value>
        public Form MainForm
        {
            get { return mainForm; }
            set { mainForm = value; }
        }

        /// <summary>
        /// Sets the project properties object. Used by the WinSAG GUI to set the 
        /// currently chosen project.
        /// </summary>
        /// <param name="pp">The project properties object.</param>
        public void setProjectProperties(ProjectProperties pp)
        {
            if (pp != null)
            {
                projectProperties = pp;                
            }

            // TODO: if pp == null, assert here.                
        }

        /// <summary>
        /// Gets or sets a value indicating whether current open project is dirty.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is dirty; otherwise, <c>false</c>.
        /// </value>
        public bool IsProjectDirty
        {
            set { projectDataDirty = value; }
            get { return projectDataDirty; }
        }

        /// <summary>
        /// Shows the preferences dialog box. Allows the user to change tool and related
        /// info.
        /// </summary>
        internal void showPreferences()
        {
            SAGPreferences sp = sagPreferences.Clone() as SAGPreferences;
            wsfPropertiesPage = new WSF_PropertiesDialog();
            
            if (null != wsfPropertiesPage)
            {
                wsfPropertiesPage.setPropertiesObject(sp);
                DialogResult dr = wsfPropertiesPage.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    // Store the values only if the user has changed them
                    if ((sp as IGridProperties).IsDirty() == true)
                    {
                        sagPreferences = null;
                        sagPreferences = sp.Clone() as SAGPreferences;
                        (sagPreferences as IGridProperties).setDirty(false);
                    }
                }
            }
        }

        /// <summary>
        /// Shows the project properties dialog box. Allows the user to set tool options.
        /// </summary>
        public void ShowProjectProperties()
        {
            ProjectProperties tempPP;
            tempPP = projectProperties.Clone() as ProjectProperties;
            wsfPropertiesPage = new WSF_PropertiesDialog();
            
            if (wsfPropertiesPage != null)
            {
                wsfPropertiesPage.setPropertiesObject(tempPP);
                DialogResult dr = wsfPropertiesPage.ShowDialog();
                if ( dr == DialogResult.OK)
                {
                    // Save the data only if user modified it and *intentionally* saved
                    if ((tempPP as IGridProperties).IsDirty() == true)
                    {
                        projectProperties = null;
                        projectProperties = tempPP.Clone() as ProjectProperties;
                        (projectProperties as IGridProperties).setDirty(false);
                        
                        // Enable the dirty flag so that no userconfigured data is lost during app_exit
                        // or prj_close
                        this.projectDataDirty = true;
                    }                    
                }
                // Ignore the modified values, if user pressed CANCEL
            }
        }

        /// <summary>
        /// Gets or sets the current project object.
        /// </summary>
        /// <value>The current project.</value>
        public ProjectProperties CurrentProject 
        {
            set 
            { 
                this.projectProperties = value; 

                // since this is a new project_properties, reset the dirty flag
                projectDataDirty = false;
            }
            get { return projectProperties; }
        }

        /// <summary>
        /// Shows the progress window. Used by time consuming code in WinSAG.
        /// </summary>
        internal void showProgressWindow()
        {
            progress = new WSF_ProgressForm(mainForm);
            
            // Show the progress bar centered in our window                        
            progress.StartPosition = FormStartPosition.CenterScreen;
            progress.Show();
        }

        /// <summary>
        /// Hides the progress window. Used by time consuming code in WinSAG.
        /// </summary>
        internal void hideProgressWindow()
        {
            if (progress != null)
            {
                progress.Close();
                progress = null;
            }
        }

        /// <summary>
        /// Gets the current tool style sheet.
        /// </summary>
        /// <returns>Full path to the style sheet</returns>
        internal string getToolStyleSheet()
        {
            if (sagPreferences != null)
            {
                return sagPreferences.StyleSheet;
            }
            return null;
        }

        internal int getToolMaxExecTime()
        {
            if (sagPreferences != null)
            {
                return sagPreferences.TimeOut;
            }
            return WSU_Literals.SAT_WAITTIME;
        }
        /// <summary>
        /// Gets the tool to execute from the SAG preferences object.
        /// </summary>
        /// <returns></returns>
        internal string getToolToExecute()
        {
            if (sagPreferences != null)
            {
                return sagPreferences.getToolToExecute();
            }
            return null;
        }
    }

    /// <summary>
    /// Window wrapper class for IWin32Window
    /// </summary>
    public class WindowWrapper : System.Windows.Forms.IWin32Window
    {
        public WindowWrapper(IntPtr handle)
        {
            _hwnd = handle;
        }

        public IntPtr Handle
        {
            get { return _hwnd; }
        }

        private IntPtr _hwnd;
    }
}